package cn.lixiaodan.hqlbuilder.gen;

import cn.lixiaodan.hqlbuilder.model.ClassModel;

public class DaoHqlGenerator extends JavaHqlGenerator {

	@Override
	public String make(ClassModel[] cmarr, String[] conditions) {
		String hql = super.make(cmarr, conditions);
		StringBuffer code = new StringBuffer(hql);
		code.append("\n");
		code.append("List<Object[]> list = this.getEntitiesByHQL(hql, rowStart, offset, map);\n");
		code.append("List result = new ArrayList();\n");
		code.append("for(int i=0; i!=list.size(); i++){\n");
		code.append("VO item = new VO();\n");
		code.append("Object[] fields = list.get(i);\n");

		int count = 0;
		for (int i = 0; i != cmarr.length; i++) {
			ClassModel cm = cmarr[i];
			for (int j = 0; j != cm.getFields().size(); j++) {
				String field = cm.getFields().get(j);
				code.append("\titem.").append(setMethod(field)).append("((String)fields[").append(count++).append("]);\n");
			}
		}
		code.append("result.add(item);\n");
		code.append("}\n");
		code.append("return result;\n");
		return code.toString();
	}

	public String setMethod(String name) {
		StringBuffer sb = new StringBuffer();
		sb.append("set");
		char[] arr = name.toCharArray();
		arr[0] = Character.toUpperCase(arr[0]);
		sb.append(arr);
		return sb.toString();
	}

}
